package com.lzm.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.github.pagehelper.PageInfo;
import com.lzm.pojo.Hosregister;
import com.lzm.service.hosRegisterService;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.lzm.vo.QueryVo;
import com.lzm.vo.ResultVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 挂号控制器
 */
@Controller
@RequestMapping("hosRegister")
@ResponseBody
public class hosRegisterController {

    @Resource
    private hosRegisterService hosRegisterService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVo<Hosregister> queryByPage(Integer pageNum, Integer pageSize, QueryVo vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Hosregister> teamPageInfo = hosRegisterService.queryByPage(pageNum, pageSize, vo);
        return new ResultVo(teamPageInfo);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVo<Hosregister> addHosregister(Hosregister hosregister) {
        hosregister.setHosR_state(0);
        hosregister.setHosR_date(new Date());
        System.out.println(hosregister);
        int i = hosRegisterService.addHosregister(hosregister);
        if (i == 1) {
            return new ResultVo<Hosregister>();
        }
        return new ResultVo<>(500, "添加失败！服务器内部异常，请稍后再试");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVo<Hosregister> queryById(@PathVariable("id") Integer hosR_id) {
        Hosregister hosregister = hosRegisterService.queryById(hosR_id);
        return new ResultVo<>(hosregister);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVo<Hosregister> updateHosRegister(@PathVariable("id") Integer hosR_id,Hosregister hosregister){
        hosregister.setHosR_id(hosR_id);
        int i = hosRegisterService.updateHosRegister(hosregister);
        if(i==1){
            return new ResultVo<Hosregister>();
        }
        return new ResultVo<>(500,"更新失败！服务器内部异常，请稍后再试");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVo<Hosregister> deleteHosRegister(@PathVariable("id") Integer hosR_id){
        int i = hosRegisterService.deleteHosRegister(hosR_id);
        if(i==1){
            return new ResultVo<Hosregister>();
        }
        return new ResultVo<>(500,"删除失败！服务器内部异常，请稍后再试");
    }
    @RequestMapping(value = "dellist",method = RequestMethod.POST)
    public ResultVo<Hosregister> deleteList(@RequestParam("ids") String ids){
        String[] split = ids.split(",");
        List<Integer> list = new ArrayList<>();
        int flag = 0;
        for (int i=0;i<split.length;i++){
            flag = hosRegisterService.deleteHosRegister(Integer.parseInt(split[i]));
        }
        if(flag<1){
            return new ResultVo<>(500,"删除失败！服务器内部异常，请稍后再试");
        }else {
        return new ResultVo<Hosregister>();
        }

    }

}
